/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.Utilities
{
    class FeedSyncMerge
    {
        static string s_LocalPath;
        static string s_IncomingPath;

        static void Main(string[] i_Arguments)
        {
            try
            {
                //  Validate arguments
                if (i_Arguments.Length < 2)
                {
                    Microsoft.CSA.FeedSync.Utilities.FeedSyncMerge.DisplayUsage(null);
                    return;
                }

                //  Get required parameters
                s_LocalPath = i_Arguments[0];
                s_IncomingPath = i_Arguments[1];

                //  Create new instance of xml document for local feed
                System.Xml.XmlDocument LocalXmlDocument = new System.Xml.XmlDocument();

                try
                {
                    //  Load the feed contents into xml document
                    LocalXmlDocument.Load(s_LocalPath);
                }
                catch (System.Exception Exception)
                {
                    System.Console.WriteLine("Exception while loading '" + s_LocalPath + "': " + Exception.Message);
                    return;
                }

                //  Create an instance of Feed
                Microsoft.CSA.FeedSync.FeedSyncLibrary.RSSFeed LocalRSSFeed = new Microsoft.CSA.FeedSync.FeedSyncLibrary.RSSFeed(LocalXmlDocument);

                //  Create new instance of xml document for incoming feed
                System.Xml.XmlDocument IncomingXmlDocument = new System.Xml.XmlDocument();

                try
                {
                    //  Load the feed contents into xml document
                    IncomingXmlDocument.Load(s_IncomingPath);
                }
                catch (System.Exception Exception)
                {
                    System.Console.WriteLine("Exception while loading '" + s_IncomingPath + "': " + Exception.Message);
                    return;
                }

                //  Create an instance of Feed
                Microsoft.CSA.FeedSync.FeedSyncLibrary.RSSFeed IncomingRSSFeed = new Microsoft.CSA.FeedSync.FeedSyncLibrary.RSSFeed(IncomingXmlDocument);

                //  Merge feeds
                LocalRSSFeed.Merge(IncomingRSSFeed);

                //  Output merged feed
                System.Console.WriteLine(LocalRSSFeed.XmlDocument.OuterXml);
            }
            catch (System.Exception Exception)
            {
                System.Console.WriteLine("Exception encountered: " + Exception.Message);
            }
        }

        static void DisplayUsage(string i_Text)
        {
            string Text = "Usage:\r\nFeedSyncMerge [SourcePath] [MergePath]\r\n\r\nParameters:\r\n  SourcePath=fully qualified filename of source SSE document\r\n  MergePath=fully qualified filename of merge SSE document\r\n";

            //  If text was provided, prepend it to default usage text
            if (!System.String.IsNullOrEmpty(i_Text))
                Text = i_Text + "\r\n\r\n" + Text;

            System.Console.WriteLine(Text);
        }
    }
}
